package com.wyl.ui

import android.graphics.drawable.TransitionDrawable
import android.os.Bundle
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.ResourcesCompat

/**
 * TransitionDrawable 的使用
 */
class MainActivity : AppCompatActivity() {
    private lateinit var constraintLayout: LinearLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val transition = ResourcesCompat.getDrawable(
            resources,
            R.drawable.expand_collapse,
            null
        ) as TransitionDrawable

        // Instantiate an ImageView and define its properties
        val i = ImageView(this).apply {
            setImageDrawable(transition)
            contentDescription = "ic_launcher_foreground"
            // set the ImageView bounds to match the Drawable's dimensions
            adjustViewBounds = true
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
        }

        val b = Button(this).apply {
            text = "开始"
            setOnClickListener {
                transition.startTransition(1000)
            }
        }

        val b1 = Button(this).apply {
            text = "反转"
            setOnClickListener {
                transition.reverseTransition(1000)
            }
        }

        val b2 = Button(this).apply {
            text = "重置"
            setOnClickListener {
                transition.resetTransition()
            }
        }

        // Create a ConstraintLayout in which to add the ImageView
        constraintLayout = LinearLayout(this).apply {
            orientation = LinearLayout.VERTICAL
            // Add the ImageView to the layout.
            addView(i)
            addView(b)
            addView(b1)
            addView(b2)
        }

        // Set the layout as the content view.
        setContentView(constraintLayout)
    }
}
